<?php
/**
* 取得广告列表
*
* 通过该标签可以获得网站的广告列表
*
* @author Steven
* @version $Id:2009-3-30 11:24 $
* @package 2.4Final
*/
function smarty_function_ul_get_ad($params){
	global $g_db, $tb_prefix;
	global $smarty, $theme_name;
	$conditions = array();
	$limit = null;
	$conditions[] = "status=1";
	$tpl_file = (isset($params['templet']))?$params['templet']:"block.default.image.html";
	extract($params);
	if (!class_exists("Adses")) {
		uses("ad");
		$ad = new Adses();
	}else{
	    $ad = new Adses();
	}
	$fields = "title as LinkTitle,source_url as ItemPicture,id as LinkId,target_url as TargetUrl,alt_words as ImageAlert";
	if(isset($params['id'])){
		$result = $ad->read($fields, intval($params['id']));
	}else{
	    if (isset($params['type_id'])) {
	       $conditions[] = "adzone_id=".$params['type_id'];
	       //取得该zone的高度和宽度。
	       $zone_res = $g_db->GetRow("select width,height from ".$tb_prefix."adzones where id=".$params['type_id']);
	       $max_width = "100%";
	       $max_height = $zone_res['height'];
	       unset($zone_res);
	    }
		$ad->setLimit($params['row'], $params['col'], $params['max']);
		if (isset($params['orderby'])) {
			$orderby = " order by ".trim($params['orderby']);
		}else{
		    $orderby = " order by priority desc,id desc";
		}
		$tmp_cond = implode(" and ", $conditions);
		$sql = "select ".$fields." from ".$ad->getTable(true)." where ".$tmp_cond.$orderby.$ad->getLimit();
		$result = $g_db->GetArray($sql);
	}
	$output = null;
	if(!empty($result)){
	for($i=0; $i<count($result); $i++) {
	    $url = $result[$i]['TargetUrl'];
	    $op = $smarty->fetch($theme_name."/".$tpl_file, null, null, false);
	    if (empty($max_width)) {
	    $op = str_replace(array("[link:title]", "[field:title]", "[img:src]"), array($url, $result[$i]['LinkTitle'], $result[$i]['ItemPicture']), $op);
	    }else{
	    $op = str_replace(array("[link:title]", "[field:title]", "[img:src]", "[img:width]", "[img:height]"), array($url, $result[$i]['LinkTitle'], $result[$i]['ItemPicture'], $max_width, $max_height), $op);
	    }
	    $output.=$ad->checkTerminal($i);
	    $output.=$op;
	}
	}else{
	    $output = "<td>Advertisement Here</td>";
	}
	echo $output;
}
?>